package com.jumper.study.front.controller;

import org.springframework.web.bind.annotation.RestController;

import com.jumper.study.common.api.CommonPageResult;
import com.jumper.study.common.api.CommonResult;
import com.jumper.study.common.util.AdminUtil;
import com.jumper.study.db.model.Account;
import com.jumper.study.front.dto.req.LessonSearchReq;
import com.jumper.study.front.dto.resp.LessonCategoryResp;
import com.jumper.study.front.dto.resp.LessonChapterResp;
import com.jumper.study.front.dto.resp.LessonDetailResp;
import com.jumper.study.front.dto.resp.LessonEbookResp;
import com.jumper.study.front.dto.resp.LessonListItemResp;
import com.jumper.study.front.dto.vo.PageVO;
import com.jumper.study.front.service.LessonService;
import com.jumper.study.front.service.StudentLessonService;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;



@RestController("frontLessonController")
@RequestMapping("/api/front/lesson")
public class LessonController {
    @Autowired
    private LessonService lessonService;

    @Autowired
    private AdminUtil adminUtil;

    @Autowired
    private StudentLessonService studentLessonService;
    
    /**
     * 课程分类
     * @return
     */
    @GetMapping("/category")
    public CommonResult<List<LessonCategoryResp>> categories() {
        List<LessonCategoryResp> categories = lessonService.categoryListWithChildren();
        return CommonResult.success(categories);
    }
    
    /**
     * 课程查询
     * @param req
     * @return
     */
    @PostMapping("/")
    public CommonPageResult<List<LessonListItemResp>> index(@RequestBody LessonSearchReq req) {
        PageVO page = new PageVO(req.getPage(), req.getLimit());
        List<LessonListItemResp> resp = lessonService.pageSearchList(page, req);
        return CommonPageResult.restPage(page.getPage(), page.getPageSize(), page.getTotal(), resp);
    }
    
    /**
     * 课程详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult<LessonDetailResp> detail(@PathVariable Long id) {
        LessonDetailResp resp = lessonService.getDetail(id);
        if (resp == null) {
            return CommonResult.notFound("课程不存在");
        }

        if (adminUtil.isLogged()) {
            Account account = adminUtil.getAccount();
            boolean isBuyed = studentLessonService.isBuyed(id, account.getId());
            resp.setIsBuyed(isBuyed);
        }
        return CommonResult.success(resp);
    }
    
    /**
     * 章节列表
     * @param id
     * @return
     */
    @GetMapping("/{id}/chapters")
    public CommonResult<List<LessonChapterResp>> chapters(@PathVariable Long id) {
        List<LessonChapterResp> chapters = lessonService.chapterList(id);
        return CommonResult.success(chapters);
    }
    
    /**
     * 课件列表
     * @param id
     * @return
     */
    @GetMapping("/{id}/ebooks")
    public CommonResult<List<LessonEbookResp>> ebooks(@PathVariable Long id) {
        List<LessonEbookResp> ebooks = lessonService.ebookList(id);
        return CommonResult.success(ebooks);
    }
    
}
